package cn.i96688.scanclient.ui;

import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileFilter;

import cn.i96688.scanclient.bean.PayLog;

@SuppressWarnings("serial")
public class DebugDialog extends JDialog{
	private JCheckBox debug;
	private JButton export;
	private JTextArea logTxt;
	public DebugDialog(JFrame frame){
		super(frame, true);
		setLayout(new BorderLayout());
		setTitle("调试设置");
		setIconImage(MainForm.icon);
		
		SuperPane mainPane=new SuperPane();
		add(new JLabel(" "),BorderLayout.WEST);
		add(new JLabel(" "),BorderLayout.EAST);
		
		mainPane.setLayout(new GridBagLayout());
//		mainPane.setBackground(Color.GRAY);
		debug=new JCheckBox("调试");
		export=new JButton("导出日志");
		JLabel logLab=new JLabel("日志内容");
		logTxt=new JTextArea();
		logTxt.setRows(6);
		mainPane.add(debug,new GBC(0,0).setAnchor(GBC.FIRST_LINE_START).setFill(GBC.HORIZONTAL).setInsets(2));
		mainPane.add(new JLabel("  "),new GBC(1,0).setInsets(0,20,0,20));
		mainPane.add(export,new GBC(2,0).setAnchor(GBC.FIRST_LINE_START).setInsets(2));
		
		add(mainPane,BorderLayout.NORTH);
		JScrollPane scroll = new JScrollPane(logTxt);
		add(scroll,BorderLayout.CENTER);
		
		init();
	}
	private void init(){
		debug.setSelected(PayLog.getInstance().cache);
		debug.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				PayLog.getInstance().cache=debug.isSelected();
				setLogText();
			}});
		export.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser dlg = new JFileChooser();
				dlg.setFileFilter(new FileFilter(){
					@Override
					public boolean accept(File f) {
						if (f.isDirectory())return true;
					    return f.getName().toLowerCase().endsWith(".txt");  //设置为选择以.txt为后缀的文件
					}

					@Override
					public String getDescription() {
						return ".txt";
					}});
				dlg.setDialogTitle("保存日志");
				int result = dlg.showSaveDialog(DebugDialog.this);
				if(result==JFileChooser.SAVE_DIALOG){
					File  file=dlg.getSelectedFile();
					if(file==null || file.getPath().trim().equals("")){
						JOptionPane.showMessageDialog(DebugDialog.this, "请选择正确的文件", "保存文件失败", JOptionPane.ERROR_MESSAGE);
						return;
					}
				}
			}});
		setLogText();
	}
	
	private void setLogText(){
		if(PayLog.getInstance().cache){
			logTxt.setText("");
			for(String s:PayLog.getInstance().getLogs()){
				logTxt.append(s+"\n");
			}
		}
	}
	
	public void showForm() {
		int width=500;
		int height=200;
		Rectangle rect = getParent().getBounds();
		int x = rect.x + (rect.width - width) / 2;
		int y = rect.y + (rect.height - height) / 2;
		this.setBounds(x, y, width, height);
		setVisible(true);
	}
}
